package com.hontech.bread.ui.dialog

import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.TextView
import com.hontech.bread.R
import com.hontech.bread.app.AppContext
import com.hontech.bread.store.StoreProduct
import com.hontech.bread.store.setProductImage
import com.hontech.bread.ui.BaseActivity
import com.hontech.bread.utils.PayManager
import com.hontech.bread.utils.isBigScreen
import com.hontech.bread.utils.priceFormat
import com.hontech.bread.utils.runOnUiCatchAny

private fun layout(): Int {
    return if (isBigScreen()) {
        R.layout.dialog_pay_verify_big
    } else {
        R.layout.dialog_pay_verify
    }
}

private fun width(): Int {
    return if (isBigScreen()) {
        500
    } else {
        ViewGroup.LayoutParams.MATCH_PARENT
    }
}

private fun height(): Int {
    return if (isBigScreen()) {
        700
    } else {
        return ViewGroup.LayoutParams.MATCH_PARENT
    }
}

private class PayVerifyDialog(val product: StoreProduct) {

    private val view = LayoutInflater.from(AppContext).inflate(layout(), null)
    private val window = PopupWindow(
        view,
        width(),
        height(),
        false
    )

    private val mImageView = view.findViewById<ImageView>(R.id.id_pay_verify_image)
    private val mBtnCancel = view.findViewById<Button>(R.id.id_pay_verify_btn_cancel)
    private val mTvName = view.findViewById<TextView>(R.id.id_pay_verify_tv_name)
    private val mTvPrice = view.findViewById<TextView>(R.id.id_pay_verify_tv_price)
    private val mTvPrice2 = view.findViewById<TextView>(R.id.id_pay_verify_tv_price2)
    private val mTvRecv = view.findViewById<TextView>(R.id.id_pay_verify_tv_received)
    private val mTvChange = view.findViewById<TextView>(R.id.id_pay_verify_tv_change)

    init {
        window.animationStyle = R.style.PopupTheme

        mImageView.setProductImage(product)
        mTvName.text = product.name
        mTvPrice.text = priceFormat(product.price.toLong())
        mTvPrice2.text = priceFormat(product.price.toLong())
        mTvChange.text = "Give change:${priceFormat(0)}"
        mTvRecv.text = "Payment received:${priceFormat(0)}"
    }

    suspend fun wait(view: View): PayManager {
        var chang = 0
        var recv = 0

        val manager = PayManager(product.price, {
            chang += it
            mTvChange.text = "Give change:${priceFormat(chang.toLong())}"
        }, {
            recv += it.value
            mTvRecv.text = "Payment received:${priceFormat(recv.toLong())}"
        })

        if (isBigScreen()) {
            window.showAtLocation(view, Gravity.CENTER_HORIZONTAL or Gravity.TOP, 0, 150)
        } else {
            window.showAtLocation(view, Gravity.CENTER, 0, 0)
        }


        mBtnCancel.setOnClickListener {
            runOnUiCatchAny { manager.cancel() }
        }

        try {
            manager.waitPay()
        } finally {
            window.dismiss()
        }

        return manager
    }
}

private var showFlag = false

fun hasPaying() = showFlag

suspend fun waitVerify(product: StoreProduct): PayManager {
    showFlag = true
    try {
        return PayVerifyDialog(product).wait(BaseActivity.parentView())
    } finally {
        showFlag = false
    }
}






